package com.wcfsy.wechat.wx.controller;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wcfsy.wechat.wx.entity.Report;
import com.wcfsy.wechat.wx.entity.StatisticsEnum;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.ReportMapper;
import com.wcfsy.wechat.wx.util.DateUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;

@Controller
@RequestMapping("/cms/report/")
public class ReportUserController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private ReportMapper reportMapper;
	
	@RequestMapping("reportData")
	@ResponseBody
	public IPage<Report> reportData(String type,String startDate,String endDate,int page, int rows){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		Map<String,Object> mapPhwechat = new HashMap<String, Object>(); 
		if(StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)){
			if(!DateUtil.compare_date(startDate, endDate)){
				return new Page<Report>(null,0, page, rows);
			}
			paramMap.put("startDate", startDate);
			paramMap.put("endDate", endDate);
		}
		List<Report> reports = reportMapper.queryReport(paramMap);
		int lenth = reports.size() > 0 ? reports.size():0;
		if(!reports.isEmpty() && lenth > 0){
			int userCountTotal = 0;
			int loanTotal = 0;
			int userTotal = 0;
			int repaymentUser = 0;
			double loanamount  = 0.00;
			double repaymentamount = 0.00;
			for (Report report : reports) {
				if(report.getType().equals(StatisticsEnum.PHWECHAT.getCode())){
					userCountTotal += report.getTotalUser();
					report.setType(StatisticsEnum.PHWECHAT.getDesc());
				}else if(report.getType().equals(StatisticsEnum.LOAN.getCode())){
					loanTotal += report.getTotalUser();
					report.setType(StatisticsEnum.LOAN.getDesc());
				}else if(report.getType().equals(StatisticsEnum.USER.getCode())){
					userTotal += report.getTotalUser();
					report.setType(StatisticsEnum.USER.getDesc());
				}else if(report.getType().equals(StatisticsEnum.REPAYMENTUSER.getCode())){
					repaymentUser += report.getTotalUser();
					report.setType(StatisticsEnum.REPAYMENTUSER.getDesc());
				}else if(report.getType().equals(StatisticsEnum.LOANAMOUNT.getCode())){
					loanamount += report.getAmountOfMoney();
					report.setType(StatisticsEnum.LOANAMOUNT.getDesc());
				}else if(report.getType().equals(StatisticsEnum.REPAYMENTAMOUNT.getCode())){
					repaymentamount += report.getAmountOfMoney();
					report.setType(StatisticsEnum.REPAYMENTAMOUNT.getDesc());
				}
			}
			
			mapPhwechat.put("userCountTotal", userCountTotal);
			mapPhwechat.put("loanTotal", loanTotal);
			mapPhwechat.put("userTotal", userTotal);
			mapPhwechat.put("repaymentUser", repaymentUser);
			if(loanamount == 0){
				mapPhwechat.put("loanamount", "0.00");
			}else{
				mapPhwechat.put("loanamount",new DecimalFormat("#.00").format(loanamount));
			}
			if(repaymentamount == 0){
				mapPhwechat.put("repaymentamount", "0.00");
			}else{
				mapPhwechat.put("repaymentamount",new DecimalFormat("#.00").format(repaymentamount));
			}
		}
		Page<Report> pages = new Page<Report>(reports,reportMapper.count(paramMap), page, rows);
		pages.setMap(mapPhwechat);
		return pages;
	}
	
	@RequestMapping("reports")
	@ResponseBody
	public JsonMessage reports(String type,String startDate,String endDate){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		Map<String,Object> mapPhwechat = new HashMap<String, Object>(); 
		if(StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)){
			if(!DateUtil.compare_date(startDate, endDate)){
				return JsonMessage.getErrorJson("时间不对");
			}
			paramMap.put("startDate", startDate);
			paramMap.put("endDate", endDate);
		}
		List<Report> reports = reportMapper.queryReport(paramMap);
		int lenth = reports.size() > 0 ? reports.size():0;
		if(!reports.isEmpty() && lenth > 0){
			int userCountTotal = 0;
			int loanTotal = 0;
			int userTotal = 0;
			int repaymentUser = 0;
			double loanamount  = 0.00;
			double repaymentamount = 0.00;
			for (Report report : reports) {
				if(report.getType().equals(StatisticsEnum.PHWECHAT.getCode())){
					userCountTotal += report.getTotalUser();
				}else if(report.getType().equals(StatisticsEnum.LOAN.getCode())){
					loanTotal += report.getTotalUser();
				}else if(report.getType().equals(StatisticsEnum.USER.getCode())){
					userTotal += report.getTotalUser();
				}else if(report.getType().equals(StatisticsEnum.REPAYMENTUSER.getCode())){
					repaymentUser += report.getTotalUser();
				}else if(report.getType().equals(StatisticsEnum.LOANAMOUNT.getCode())){
					loanamount = loanamount + report.getAmountOfMoney();
				}else if(report.getType().equals(StatisticsEnum.REPAYMENTAMOUNT.getCode())){
					repaymentamount = repaymentamount + report.getAmountOfMoney();
				}
			}
			
			mapPhwechat.put("userCountTotal", userCountTotal);
			mapPhwechat.put("loanTotal", loanTotal);
			mapPhwechat.put("userTotal", userTotal);
			mapPhwechat.put("repaymentUser", repaymentUser);
			logger.info("{"+loanamount+"," + repaymentamount+"}");
			if(loanamount == 0.00){
				mapPhwechat.put("loanamount", "0.00");
			}else{
				mapPhwechat.put("loanamount",new DecimalFormat("#.00").format(loanamount));
			}
			if(repaymentamount == 0.00){
				mapPhwechat.put("repaymentamount", "0.00");
			}else{
				mapPhwechat.put("repaymentamount",new DecimalFormat("#.00").format(repaymentamount));
			}
			logger.info("{"+mapPhwechat.get("loanamount")+"," + mapPhwechat.get("repaymentamount")+"}");
		}
		return JsonMessage.getSuccessJson(mapPhwechat);
	}
	
	
	
	@RequestMapping("view/page")
	public String page(ModelMap modelMap) {	
		modelMap.put("statistics", StatisticsEnum.values());
		
		
		return "report/reportList";
	}
}
